package com.example.aircraftwar2024.leaderBoard;
import java.io.*;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.text.*;

public class ScoreBoardImpl implements ScoreBoardDAO{
    private List<ScoreRecord>records=new LinkedList<>();
    private static final File easyfile=new File("/data/data/com.example.aircraftwar2024/files/score_easy.txt");
    private static final File normalfile=new File("/data/data/com.example.aircraftwar2024/files/score_normal.txt");
    private static final File hardfile=new File("/data/data/com.example.aircraftwar2024/files/score_hard.txt");
    private File file;

    public void setFile(int gameMode){
        if ( gameMode==0) file=easyfile;
        else if ( gameMode==1) file=normalfile;
        else file=hardfile;
    }
    public void readFromFile(){
        try(BufferedReader bufferedReader=new BufferedReader(new FileReader(file))){
            String line;
            while ((line=bufferedReader.readLine())!=null){
                String[] ret=line.split(" ");
                if (ret.length!=3) {
                    System.out.println("Wrong Score Format! ");
                }
                int score=Integer.parseInt(ret[1]);
                records.add(new ScoreRecord(ret[0],score,new Date(Long.parseLong(ret[2]))));
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        records=records.stream().sorted(Comparator.comparing(ScoreRecord::getScore).reversed().thenComparing(ScoreRecord::getTime)).collect(Collectors.toList());
    }
    public List<ScoreRecord> getAllRecord(){
        return records;
    };
    public void doAdd(int score,String name){
        records.add(new ScoreRecord(name,score,new Date()));
        records=records.stream().sorted(Comparator.comparing(ScoreRecord::getScore).reversed().thenComparing(ScoreRecord::getTime)).collect(Collectors.toList());
        outputToFile();
    };
    public void doDelete(int rank){
        int rk=1;
        for (ScoreRecord item:records){
            if (rk==rank) {
                records.remove(item);
                outputToFile();
                return;
            }
            rk++;
        }
    };
    public void outputToFile(){
        try(BufferedWriter fOut=new BufferedWriter(new FileWriter(file))){
            for (ScoreRecord item: records){
                String score=(new String()).valueOf(item.getScore());
                String line=item.getName()+' '+score+" "+(new String()).valueOf(item.getTime().getTime());
                fOut.write(line);
                fOut.newLine();
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }

    };
    public void outputToConsole(){
        int rk=1;
        System.out.println("****************************\n" +
                "           得分记录         \n"+
                "****************************\n");
        for (ScoreRecord item:records){

            SimpleDateFormat ft = new SimpleDateFormat ("MM-dd hh:mm");
            String line="第"+rk+"名: "+item.getName()+", "+item.getScore()+", "+ft.format(item.getTime());
            System.out.println(line);
            rk++;
        }
    };
    public String[][] outputToStringArray(){
        String[][] ret=new String[records.size()][4];
        int rk=0;
        for (ScoreRecord item:records){

            SimpleDateFormat ft = new SimpleDateFormat ("MM-dd hh:mm");
            String line="第"+rk+"名: "+item.getName()+", "+item.getScore()+", "+ft.format(item.getTime());
            ret[rk][0]="第"+(rk+1)+"名: ";
            ret[rk][1]=item.getName();
            ret[rk][2]=""+item.getScore();
            ret[rk][3]=ft.format(item.getTime());
            rk++;
        }
        return ret;
    }
}
